<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Bonus extends Common
{
    /**
     * User:Simony
     * Function: 奖金表列表
     * @return mixed
     */
    public function bonusList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_op = input('get.search_op', 'eq');
        $search_value = input('get.search_value', '');
        $date_range = input('get.date_range', '');

        //where
        $where = [];

        //judge
       if($field && $value){
           if($field=='m.id'){
               $where[] = [$field,'eq',$value];
           }else{
               $where[] = [$field, 'like', '%' .$value . '%'];
           }
       }
        $search_field && $search_value && $search_op && $where[] = [$search_field, $search_op, $search_value];



        $name = [];
        $start_time = $end_time = '';
        if (!empty($date_range)) {
            $date_range = explode(' - ', $date_range);
            $start_time = strtotime($date_range[0]);
            $end_time = strtotime($date_range[1]) + 86399;
            $name['begin_date'] = $date_range[0];
            $name['end_date'] = $date_range[1];
        }
        if (!(empty($start_time)) && !(empty($end_time))) {
            $where[] = ['b.create_time', 'egt', $start_time];
            $where[] = ['b.create_time', 'elt', $end_time];
        }

        //model
        $bonusModel = model('common/bonus/Bonus');

        //getData
        $list = $bonusModel->getBonusList($where, 10,'b.*,m.nickname,m.username');

        //assign
        $this->assign('name', $name);
        $this->assign('list', $list);
        $this->assign('page', $bonusModel->page_info->render());
        $this->assign('grant_types', $bonusModel->grant_types);
        return $this->fetch();
    }

    /**
     * User: Simony
     * Function:
     */
    public function source()
    {

        $date_range = input('get.date_range', '');

        $name = [];
        $start_time = $end_time = '';
        if (!empty($date_range)) {
            $date_range = explode(' - ', $date_range);
            $start_time = strtotime($date_range[0]);
            $end_time = strtotime($date_range[1]) + 86399;
            $name['begin_date'] = $date_range[0];
            $name['end_date'] = $date_range[1];
        }


        $where = array();
        if (!(empty($start_time)) && !(empty($end_time))) {
            $where[] = ['b.create_time', 'egt', $start_time];
            $where[] = ['b.create_time', 'elt', $end_time];
        }

        $field = "b.periods,done,b.create_time,b.grant_time,SUM(b.money) AS money,SUM(b.netincome) AS netincome";
        $field .= ",SUM(b.recommend_money) as recommend_money,SUM(b.commission_money) as commission_money,SUM(b.partner_money) as partner_money";

        $bonusModel = model('common/bonus/Bonus');

        $res = $bonusModel->alias('b')
            ->field($field)
            ->where($where)
            ->group(' b.periods,b.done')
            ->order('b.periods desc')
            ->paginate(10, false, ['query' => request()->param()]);

        $this->assign('name', $name);
        $this->assign('list', $res->items());
        $this->assign('page', $res->render());
        return $this->fetch();
    }

}